﻿const router = require('koa-router')()
const GroupModel = require('../models/group')
const routes = require('../config/config_test')

router.prefix('/api')

//设置用户组权限
router.post("/setGroup", async (ctx) => {
    ctx.status = 200
    let data = ctx.request.body
    let { id, keyword } = data
    // if (keyword <= 2) {
    //     ctx.status = 403
    //     ctx.body = {
    //         code: 403,
    //         msg: "不正规的修改请求"
    //     }
    //     return
    // }
    let res = await GroupModel.findOneAndUpdate({ id }, { $set: { role_status: keyword } })
    if (res) {
        ctx.body = {
            code: 200,
            msg: "用户信息修改成功",
            res: res
        }
    }
})


//获取所有权限分组
router.get('/getGroup',async (ctx) => {
    ctx.status = 200

    let res = await GroupModel.find({},{routes:false,_id:false})

    ctx.body = {
        code:200,
        data:res
    }
})


//添加权限分组
router.post('/addGroup',async (ctx) => {
    ctx.status = 200

    let {root,role_code,role_des} = ctx.request.body

    let res = await GroupModel.find({})

    let id = 1
    if(res.length != 0){
        id = res[res.length-1].id+1
    }

    await GroupModel.create({id,root,routes,role_code,role_des})

    ctx.body = {
        code:200,
        msg:"添加角色成功"
    }
})

//编辑角色基本信息
router.post('/editGrop',async (ctx) => {
    ctx.status = 200

    let {id,role_des,root,role_code} = ctx.request.body

    let res = await GroupModel.findOneAndUpdate({id:id},{$set:{root:root,role_code:role_code,role_des:role_des}})

    console.log(res);
    if(res){
        ctx.body = {
            code:200,
            msg:"设置成功"
        }
    }else {
        ctx.body = {
            code:200,
            msg:"设置失败"
        }
    }
})

//查询角色
router.post('/queryGroup',async (ctx) => {
    ctx.status = 200

    let data = ctx.request.body

    let res = await GroupModel.findOne(data)

    ctx.body = {
        code:200,
        data:res
    }
})


//删除角色
router.post('/deleteGroup',async (ctx) => {
    ctx.status = 200

    let {id} = ctx.request.body

    let res = await GroupModel.findOneAndDelete({id})

    if(res != null){
        ctx.body = {
            code:200,
            msg:"删除成功"
        }
    }else {
        ctx.body = {
            code:200,
            msg:"删除失败"
        }
    }
})


//获取角色的路由菜单
router.post('/sarcharUserRoutes',async (ctx) => {
    ctx.status = 200
    let {id} = ctx.request.body;
    let res=await GroupModel.find({})
    let Roleroutes= await GroupModel.findOne({id:id})
    let arr=[]
    let arr1=[]
    Roleroutes.routes.forEach(item=>{
        console.log(item.id);
        arr.push(item.id)
     item.subitem.forEach(item1=>{
         console.log(item1.id);
         arr1.push(item1.id)
     })
    })
        console.log(arr);
        let arr2=arr.concat(arr1)
    ctx.body={
       code:200,
       systemPermissions:res[0],
       assignedPermissions: arr2
        
   }
})
 //获取角色的路由菜单,前段路由判断
router.post('/getGroupRoutes',async (ctx) => {
    ctx.status = 200

    let {id} = ctx.request.body

    let res = await GroupModel.findOne({id})

    ctx.body = {
        code:200,
        data:res.routes
    }
})


//修改角色的菜单路由
router.post('/editRoutes',async (ctx) => {
    ctx.status = 200

    let {id} = ctx.request.body
    let res = await GroupModel.findOneAndUpdate({id},{$set:{routes:data_routes}})

    let res1 = await GroupModel.findOne({id})

    if(res != null){
        ctx.body = {
            code:200,
            msg:"修改角色菜单成功",
            data:res1.routes
        }
    }else{
        ctx.body = {
            code:200,
            msg:"修改角色菜单失败",
        }
    }
})

//修改角色的菜单路由
router.post('/setRolerRoutes',async (ctx) => {
    ctx.status = 200

    let {id,permissions} = ctx.request.body
        if(id==1){
            ctx.body={
                code:400,
                msg:"无法更改管理员权限"
            }
        }else{
            let aaa=await GroupModel.find({})
                let res1 = await GroupModel.findOne({id})
                        // console.log(res1);
                        // 遍历所有路由
                        let arr=[]
                    aaa[0].routes.forEach(item=>{
                        // console.log(item);
                        permissions.forEach(i=>{
                            if(item.id==i){
                                arr.push(item)
                            }
                        })
                    })
                    console.log(arr);
            let res = await GroupModel.findOneAndUpdate({id},{$set:{routes:arr}})
                        ctx.body={
                            code:200,
                            msg:"设置菜单成功"
                        }
        }

})

// 菜单管理页面

router.get('/getRoleRoutesMenu',async (ctx) => {
    ctx.status = 200

    let res = await GroupModel.findOne({})
        ctx.body={
            code:200,
            data:res.routes
        }
    
})


module.exports = router
